///////////////////////////////////////////////////////////////
// Copyright(c) BeingStudio
// 
// FileName : ClientManager
// Creator  : ChenShiCai
// E-Mail   : BeingStudio@GMail.com
// Date     : 2008-08-31
// Comment  : 
//
///////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "ClientManager.h"

#define APP_CLASS       _T("GameMaffia")
#define APP_NAME        _T("GameMaffia")
#define APP_ICON        0
#define APP_ACCEL       0
ClientManager *g_pClient = NULL;

ClientManager::ClientManager()
{
    g_pClient = this;
    m_hInstance = NULL;
    m_hDC = NULL;
    m_nExitFlag = false;

    memset(m_pfOnSystemMSG, 0, sizeof(m_pfOnSystemMSG));
    m_pfOnSystemMSG[WM_CREATE] = &ClientManager::OnCreate;
    m_pfOnSystemMSG[WM_PAINT] = &ClientManager::OnPaint;
    m_pfOnSystemMSG[WM_DESTROY] = &ClientManager::OnDestroy;
}

ClientManager::~ClientManager()
{
    memset(m_pfOnSystemMSG, 0, sizeof(m_pfOnSystemMSG));
}

int ClientManager::Init(HINSTANCE hInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	int nResult = false;    
	int nRetCode = false;

    int nClassInitFlag = false;
    int nWindowInitFlag = false;

    PROCESS_ERROR(hInstance);  
    m_hInstance = hInstance;


    nRetCode = InitClass();
    PROCESS_ERROR(nRetCode);
    nClassInitFlag = true;

    nRetCode = InitWindow(nCmdShow);
    PROCESS_ERROR(nRetCode);
    nWindowInitFlag = true;
	
   
	nResult = true;
Exit0:
    if (!nResult)
    {
        m_nExitFlag = true;

        if (nWindowInitFlag)
            UnInitWindow();

        if (nClassInitFlag)
            UnInitClass();

        g_pClient = NULL;
    }
	return nResult;
}

void ClientManager::UnInit()
{
    m_nExitFlag = true;

    UnInitWindow();
    UnInitClass();
    m_hInstance = NULL;
    g_pClient = NULL;
}

int ClientManager::Run() 
{
	int nResult = false;    
	int nRetCode = false;  
    MSG msg;

    while (!m_nExitFlag)
    {
        nRetCode = GetMessage(&msg, NULL, 0, 0);
        PROCESS_ERROR(nRetCode != -1);
        if (!nRetCode)
            break;

        TranslateMessage(&msg); 
        DispatchMessage(&msg);    
    }

	nResult = true;
Exit0:
	return nResult;
}

int ClientManager::InitClass()
{
    int nResult  = false;    
    int nRetCode = false;
    WNDCLASS wndClassInfo;

    ASSERT(m_hInstance);

    wndClassInfo.style = CS_HREDRAW | CS_VREDRAW;
    wndClassInfo.lpfnWndProc = ::WndProc;
    wndClassInfo.cbClsExtra = 0;
    wndClassInfo.cbWndExtra = 0;
    wndClassInfo.hInstance = m_hInstance;
    wndClassInfo.hIcon = LoadIcon(m_hInstance, MAKEINTRESOURCE(APP_ICON));;
    wndClassInfo.hCursor = LoadCursor(NULL, IDC_ARROW);;
    wndClassInfo.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wndClassInfo.lpszMenuName = NULL;
    wndClassInfo.lpszClassName = APP_CLASS;

    nRetCode = ::RegisterClass(&wndClassInfo);
    PROCESS_ERROR(nRetCode);

    nResult = true;
Exit0:
    return nResult;
}

void ClientManager::UnInitClass() 
{
    ASSERT(m_hInstance);
    UnregisterClass(APP_NAME, m_hInstance);
}


int ClientManager::InitWindow(int nCmdShow) 
{
	int nResult = false;    
	int nRetCode = false;
    HWND hWnd = NULL;
	
    ASSERT(m_hInstance);

    hWnd = CreateWindow(
        APP_CLASS, 
        APP_NAME, 
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 
        0, 
        CW_USEDEFAULT, 
        0, 
        NULL, 
        NULL, 
        m_hInstance, 
        NULL
    );
    PROCESS_ERROR(hWnd);

    nRetCode = ShowWindow(hWnd, nCmdShow);
    //PROCESS_ERROR(nRetCode);

    nRetCode = UpdateWindow(hWnd);
    PROCESS_ERROR(nRetCode);
    
	nResult = true;
Exit0:
	return nResult;
}

void ClientManager::UnInitWindow()
{
    //Do Nothing
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    ASSERT(g_pClient);
    return g_pClient->WndProc(hWnd, message, wParam, lParam);
}

LRESULT CALLBACK ClientManager::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    ON_MSG pfn = NULL;

    if (message < WM_USER)
        pfn = m_pfOnSystemMSG[message];
    
    if (!pfn)        
        return DefWindowProc(hWnd, message, wParam, lParam);

    (this->*pfn)(hWnd, wParam, lParam);
    return 0;   
}

int ClientManager::OnCreate(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
	int nResult = false;    
	int nRetCode = false;
    HDC hdc = NULL;

    hdc = GetDC(hWnd);
    PROCESS_ERROR(hdc);
    
    m_hDC = hdc;
	nResult = true;
Exit0:
    if (!nResult)
        PostQuitMessage(0);
	return nResult;
}

int ClientManager::OnPaint(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
    int nResult = false;    
    int nRetCode = false;

    nResult = true;
//Exit0:
    return nResult;
}

int ClientManager::OnDestroy(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
    int nResult = false;    
    int nRetCode = false;

    PostQuitMessage(0);

    nResult = true;
//Exit0:
    return nResult;
}

